package com.onion.utils.file;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtils {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    /**
     * 加载读取多个properties file
     * @param propertiesFiles
     * @return
     */
    public static Properties[] loadProperties(String[] propertiesFiles) {
        Properties[] properties = new Properties[propertiesFiles.length];
        for(int i=0; i<propertiesFiles.length; i++){
            properties[i] = loadProperties(propertiesFiles[i]);
        }
        return properties;
    }

    /**
     * 加载读取properties file
     * @param propertiesFile
     * @return
     */
    public static Properties loadProperties(String propertiesFile) {
        Properties props = new Properties();
        InputStream ins = null;
        try {
            ins = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesFile);
            props.load(ins);
            return props;
        } catch (FileNotFoundException e1) {
            logger.error("properties配置文件未找到:{}", propertiesFile);
            throw new RuntimeException(e1);
        } catch (IOException e2) {
            logger.error("properties配置文件IO错误:{}", propertiesFile);
            throw new RuntimeException(e2);
        } finally {
            try {
                if (ins != null) {
                    ins.close();
                }
            } catch (IOException e) {
                logger.error("properties文件流关闭出现异常:{}", propertiesFile);
            }
        }
    }

}
